﻿using System;
using System.Collections.Generic;
using System.Linq;
using EPiBoost.Core.Interfaces.ContentInterfaces;
using EPiServer;
using EPiServer.Core; 
using EPiServer.DataAbstraction; 

namespace EPiBoost.Core.Extensions
{
    public static class PageDataExtensions
	{
		#region Interfaces



		public static string GetMainTitle(this PageData page)
		{
			if(page is IMainTitle)
			{
				return (page as IMainTitle).MainTitle;
			}
			return page.PageName;
		}

		public static string GetMainShortTitle(this PageData page)
		{
			if (page is IMainShortTitle)
			{
				return (page as IMainShortTitle).MainShortTitle;
			}
			return page.GetMainTitle();
		}


		/// <summary>
		/// checks if a page implements the specified interface
		/// </summary>
		/// <param name="pageData"></param>
		/// <param name="interfaceType"></param>
		/// <returns></returns>
		public static bool ImplementsInterface(this PageData pageData, Type interfaceType)
		{
			var type = pageData.GetOriginalType();
			if (type != null)
			{
				return interfaceType.IsAssignableFrom(type);
			}

			return false;
		}

    	#endregion
		

		#region Get Published / Saved Versions
		public static PageData GetPreviouslyPublishedVersion(this PageReference pageRef)
		{
			PageVersion previousVersion = PageVersion.List(pageRef)
				.FirstOrDefault(pv => pv.Status == VersionStatus.PreviouslyPublished || pv.Status == VersionStatus.Published);

			return previousVersion != null ? DataFactory.Instance.GetPage(previousVersion.ID, LanguageSelector.AutoDetect(true)) : null;
		}

		public static PageData GetPreviouslySavedVersion(this PageReference pageRef)
		{
			PageVersion previousVersion = PageVersion.List(pageRef).FirstOrDefault();

			return previousVersion != null ? DataFactory.Instance.GetPage(previousVersion.ID, LanguageSelector.AutoDetect(true)) : null;
		}

		public static IEnumerable<PageData> GetPreviousVersions(this PageReference pageRef)
		{
			PageVersionCollection previousVersions = PageVersion.List(pageRef);

			foreach (PageVersion previousVersion in previousVersions)
			{
				yield return DataFactory.Instance.GetPage(previousVersion.ID, LanguageSelector.AutoDetect(true));
			}
		}
		#endregion

		#region Url

		/// <summary>
		/// Get the external URL for a page
		/// </summary> 
		public static string GetExternalUrl(this PageData page)
		{
			if(page == null)
			{
				return null;
			}

			Uri uri = new Uri(EPiServer.Configuration.Settings.Instance.SiteUrl, page.LinkURL);
			UrlBuilder url = new UrlBuilder(uri.AbsoluteUri);
			if (url.QueryCollection["id"] != null && int.Parse(url.QueryCollection["id"]) != page.PageLink.ID)
			{
				page.PageLink = new PageReference(int.Parse(url.QueryCollection["id"]));
			}
			EPiServer.Global.UrlRewriteProvider.ConvertToExternal(url, page.PageLink, System.Text.UTF8Encoding.UTF8);
			return url.Uri.AbsoluteUri;
		}

	 
		#endregion
	}
}
